<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Thumbnail extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(2);
		$this->load->model('Company_thumbnail_model', 'thumbnail');
		$this->lang->load('company/thumbnail', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$images_list = $this->thumbnail->get(array('idmember' => $this->session->userdata['idmember']));
		
		$grid_images = array();
        
        foreach ($images_list->result() as $key => $row)
		{
			$image = '<div class="rowblock"><span><img src="'.base_url() . "f/" . $this->session->userdata['idmember'] . '/tmb/' . $row->image.'" /></span>
			<div>';
			
			if ($row->idnow == 'Y')
			{
				$image .= $this->lang->line('thumbnail_now');
			}
			else 
			{
				$image .= '<a onclick="CompanyThumbnail.updateThumbnail('.$row->id.');">'.$this->lang->line('thumbnail_updatenow').'</a>';
			}
			
			$image .= ' &middot; <a onclick="CompanyThumbnail.deleteThumbnail('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
			 
			if ($key % 2 != 0)
			{
				$grid_images[] = $image . '</div>';
			}
			else
			{
				$grid_images[] = '<div>' . $image;
			}
			
		}
		
		if (!$grid_images) {
			 $data['grid_images'] = '<center>'.$this->lang->line('thumbnail_noresults').'</center>';
		} else {
			 $data['grid_images'] = implode('', $grid_images);
		}
		
		exitHTML($data['grid_images']);
	}
	
	public function insert()
	{
		if ($_FILES['image']['error'] == 4)
		{
			formError($this->lang->line('thumbnail_missingfield'), true, true);
		}
		
		if (!in_array($_FILES['image']['type'], array('image/gif', 'image/jpeg', 'image/png', 'image/wbmp')))
		{
			formError($this->lang->line('thumbnail_invalidimageformat'), true, true);
		}
		
		if ($_FILES['image']['size'] > (1024 * 1024 * 1)) // 1Mb
		{
			formError(sprintf($this->lang->line('thumbnail_invalidimagesize'), '1 Mb'), true, true);
		}
		
		$data['image'] = $_FILES['image']['tmp_name'];
		$data['image_name'] = $_FILES['image']['name'];
		$data['idmember'] = $this->session->userdata['idmember'];
		$data['key'] = $this->session->userdata['key'];
		
		if ($this->thumbnail->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true, true);
		}
		else
		{
			exit('<script type="text/javascript">
			parent.$("#company-thumbnail").attr("src", "'.base_url().'f/'.$this->session->userdata['idmember'].'/tmb/'.$this->session->userdata['thumbnail'].'");
			parent.CompanyThumbnail.getThumbnailGrid();
			parent.closeBox();
			</script>');
		}
	}
	
	public function update()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('images_missingfield'), true);
		}
		
		$data['idmember'] = $this->session->userdata['idmember'];
		
		if ($this->thumbnail->update_thumbnail($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('$("#company-thumbnail").attr("src", "'.base_url().'f/'.$this->session->userdata['idmember'].'/tmb/'.$this->session->userdata['thumbnail'].'");
			CompanyThumbnail.getThumbnailGrid();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('images_missingfield'));
		}
		
		if ($this->thumbnail->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CompanyThumbnail.getThumbnailGrid();');
		}
	}
	
	public function add()
	{
		header("Content-type: text/html; charset=utf-8");
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/thumbnail', $data);
	}
	
	public function capture()
	{
		header("Content-type: text/html; charset=utf-8");
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/thumbnail_capture', $data);
	}
	
	public function upload()
	{
		header("Content-type: text/html; charset=utf-8");
		if ($_POST['type'] == "pixel") {
	        $im = imagecreatetruecolor(320, 240);
	
	        foreach (explode("|", $_POST['image']) as $y => $csv) {
                foreach (explode(";", $csv) as $x => $color) {
                	imagesetpixel($im, $x, $y, $color);
                }
	        }
		} else {
	        $im = imagecreatefrompng($_POST['image']);
		}
		
		$tmp_path = FCPATH . 'tmp/' . $this->session->userdata['key'] . '_capture';
		
		if (!is_dir($tmp_path))
		{
			mkdir($tmp_path);
		}
		
		$tmp_file = $tmp_path . '/image.png';
		
		imagepng($im, $tmp_file, 9);
		
		$data['tmp_path'] = $tmp_path;
		$data['tmp_file'] = $tmp_file;
		$data['image_name'] = 'image.png';
		$data['idmember'] = $this->session->userdata['idmember'];
		$data['key'] = $this->session->userdata['key'];
		
		if ($this->thumbnail->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true, true);
		}
		else
		{
			exitJs('$("#company-thumbnail").attr("src", "'.base_url().'f/'.$this->session->userdata['idmember'].'/tmb/'.$this->session->userdata['thumbnail'].'");
					CompanyThumbnail.getThumbnailGrid();
					closeBox();');
		}
	}
}

?>